import 'package:flutter/material.dart';
import 'package:p_location/common/res/common_res.dart';
import 'package:p_location/map/constant/map_http_url.dart';
import 'package:p_location/map/res/user_path_info.dart';
import 'package:p_location/map/res/user_path_res.dart';
import 'package:p_location/map/res/user_trace_area_info.dart';
import 'package:p_location/map/res/user_trace_floor_time.dart';
import 'package:p_location/map/res/user_trace_record_item.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/net/net_constants.dart';
import 'package:p_location/net/net_listener_impl.dart';
import 'package:dio/dio.dart';
import 'package:intl/intl.dart';

class PageTraceRecord extends StatefulWidget {

  /*final List<UserTraceAreaInfo> areasFromMap;*/
  //每个楼层开始到结束的时间
  final List<FloorAndTime> listT0AndT1;

  @override
  _PageTraceRecordState createState() => _PageTraceRecordState();

  PageTraceRecord(this.listT0AndT1);


}

class _PageTraceRecordState extends State<PageTraceRecord> {
  bool isRequesting = true;
  final DateFormat formatter = new DateFormat('HH:mm:ss');
  List<FloorAndTime> areas = [];

  @override
  void initState() {
    super.initState();
    if(widget.listT0AndT1 != null){
      setState(() {
        isRequesting = false;
        areas = widget.listT0AndT1;
      });
    }/*else{
      _requestPersonPath();
    }*/
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("轨迹记录"),
        centerTitle: true,
        elevation: 0,
        brightness: Brightness.dark,
        leading: IconButton(
            tooltip: '返回',
            icon: Icon(Icons.arrow_back_ios, color: Colors.white),
            onPressed: () {
              Navigator.of(context).pop();
            }),
      ),
      body: isRequesting
          ? Center(
        child: CircularProgressIndicator(),
      )
          : Container(
              padding: EdgeInsets.only(left: 6, right: 6, top: 4, bottom: 0),
              margin: EdgeInsets.only(bottom: 10),
              child: ListView.builder(
                  itemCount: areas.length,
                  itemBuilder: (context, index) {
                    return Card(
                      child: Container(
                        padding: EdgeInsets.only(left: 10, right: 10, top: 4),
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.all(Radius.circular(5))),
                        child: ListView(
                          shrinkWrap: true,
                          physics: new NeverScrollableScrollPhysics(),
                          children: [
                            Container(
                              height: 40,
//                        padding: EdgeInsets.only(left: 10,right: 10),
                              alignment: Alignment.centerLeft,
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    "F${areas[index].floorId}",
                                    style: TextStyle(
                                        color: Color.fromRGBO(0, 0, 0, 0.45)),
                                  ),
                                 /* IconButton(
                                      onPressed: () {
                                        setState(() {
                                          areas[index].isExpand = !areas[index].isExpand ;
                                        });
                                      },
                                      icon: Icon(
                                        areas[index].isExpand  ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                                        color: Color.fromRGBO(0, 0, 0, 0.45),
                                      ))*/
                                ],
                              ),
                            ),
                            Container(
                              height: 0.5,
                              color: Color.fromRGBO(0, 0, 0, 0.1),
                              child: Text(""),
                            ),
                            Container(
                              height: 42,
                              child: Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: [
                                  Text("停留时间"),
                                  Text(getRecordTime(Record(beginTime:areas[index].beginTime,endTime:areas[index].endTime)))
                                ],
                              ),
                            ),
                            /*Offstage(
                              offstage: !areas[index].isExpand ,
                              child: *//*ListView.builder(
                                  shrinkWrap: true,
                                  physics: new NeverScrollableScrollPhysics(),
                                  itemCount: areas[index].records.length,
                                  itemBuilder: (context, index2) {
                                    return*//* Container(
                                      height: 42,
                                      child: Row(
                                        mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text("停留时间"),
                                          Text(getRecordTime(Record(beginTime:areas[index].beginTime,endTime:areas[index].endTime)))
                                        ],
                                      ),
                                    ),
                                  *//*}),*//*
                            )*/
                          ],
                        ),
                      ),
                    );
                  }),
            ),
    );
  }

  getRecordTime(Record record) {
    if(record!= null){
      String left = "";
      String right = "";
      if(record.beginTime != null){
        left = formatter.format(DateTime.fromMillisecondsSinceEpoch(record.beginTime));
      }
      if(record.endTime != null){
        right = formatter.format(DateTime.fromMillisecondsSinceEpoch(record.endTime));
      }
      return '$left - $right';
    }
//    return '${formatter.format(
//        DateTime.fromMillisecondsSinceEpoch(record.beginTime))}'
//        '-'
//        '${formatter.format(DateTime.fromMillisecondsSinceEpoch(record.endTime))}';
  }

  /*_requestPersonPath() async {
    setState(() {
      isRequesting = true;
    });
    Map<String, dynamic> params = {};
    params["sn"] = "202105280001";
    params["startTime"] = "2021-07-09 16:38:00";
    params["endTime"] = "2021-07-09 16:41:12";
    await DioHttpClient.requestPost(
        context,
        MapHttpUrl.PEOPLE_PATH_SEARCH_URL,
        params,
        NetListenerImpl((Response response) {
          isRequesting = false;
          if (response != null && response.data != null) {
            UserPathRes res = UserPathRes.fromJson(response.data);
            if (res != null && res.code == 0 && res.data != null) {
              UserPathInfo userPathInfo = res.data;
              setState(() {
                if (userPathInfo != null && userPathInfo.areas != null) {
                  areas = userPathInfo.areas;
                }
              });
            }
          }
        }, (CommonRes errorRes) {
          setState(() {
            isRequesting = false;
          });
        }),
        contentType: NetConstants.POST_TYPE_FORM);
  }*/
}
